package com.ymatou.iapi.prodstock.testcase;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.prodstock.parameter.OrderReturnCatalogDto;
import com.ymatou.iapi.prodstock.parameter.OrderReturnCatalogDto.CatalogStockListDto;
import com.ymatou.iapi.prodstock.parameter.PreOrderReduceCatalogDto.CatalogListDto;
import com.ymatou.iapi.prodstock.service.ReturnStockListForRefundBillCall;
import com.ymatou.iapi.productprice.parameter.ProductAllPriceBean;
import com.ymatou.iapi.sellerproduct.service.AddFbxProductCall;
import com.ymatou.iapi.sellerproduct.service.ChangeNewProductInfoCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.ProdStockCallService;
import com.ymttest.business.service.ProductPriceCallService;
import com.ymttest.business.service.SellerProductCallService;
import com.ymttest.database.model.YmtCatalogsI;
import com.ymttest.database.mongo.jproduct.ActivityProductsWapper;
import com.ymttest.database.sqlwapper.YmtCatalogsIWapper;
import com.ymttest.database.sqlwapper.YmtProductActivityStockWapper;
import com.ymttest.database.sqlwapper.YmtProductsIWapper;
import com.ymttest.database.sqlwapper.YmtProductsInActivityWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.MapUtil;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.YMTDateUtil;

import cn.ymatou.activityadmin.service.CloseProductActivityCall;

public class Ts_ReturnStockListForRefundBill {
	// default data
	private static String sellerName = EnvSetup.getData("productsellername");
	private static int sellerId = Integer.parseInt(EnvSetup.getData("productsellerid"));

	private static YmtCatalogsIWapper ymtCatalogsIWapper = new YmtCatalogsIWapper();
	private static ActivityProductsWapper activityProductsWapper = new ActivityProductsWapper();
	private static YmtProductActivityStockWapper ymtProductActivityStockWapper = new YmtProductActivityStockWapper();

	private static ReturnStockListForRefundBillCall returnStockListForRefundBillCall = new ReturnStockListForRefundBillCall();

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("订单取消返还库存");
	}

	@Before
	public void caseUp() {
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(returnStockListForRefundBillCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}     

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_ReturnStockListForRefundBill_001() {
		Logger.start(true, "普通现货商品,返库存");
		try{
			String productId = SellerProductCallService.addNewProductOnSale(sellerId, sellerName, null).getJSONObject("Data").getString("ProductID");
			
			YmtCatalogsI ymtCatalog = ymtCatalogsIWapper.selectCatalogsByProductId(productId).get(0);
			String catalogId = ymtCatalog.getsCatalogId();
			int catalogNum = ymtCatalog.getiNum();
			int stockNum = 2;
			
			CatalogListDto catalogListDto = ProdStockCallService.ReduceStockListForPreOrder(productId, catalogId, sellerId, 0, stockNum, 0, true);
			ProdStockCallService.checkReduceStock(catalogListDto,catalogNum-stockNum,0);
			
			
			//反库存
			long refundBillNo = getRefundBillNo();
			OrderReturnCatalogDto.CatalogStockListDto catalogStockListDto = new OrderReturnCatalogDto.CatalogStockListDto();
			catalogStockListDto.setRefundBillNo(refundBillNo);
			catalogStockListDto.setBuyerId(catalogListDto.getBuyerId());
			catalogStockListDto.setOrderId(catalogListDto.getPreOrderId());
			catalogStockListDto.setProductId(productId);
			catalogStockListDto.setCatalogId(catalogId);
			catalogStockListDto.setSellerId(sellerId);
			catalogStockListDto.setStockNum(stockNum-1);
			OrderReturnCatalogDto.CatalogStockListDto catalogStockListDtoNew = ProdStockCallService.ReturnStockListForRefundBill(catalogStockListDto);
			//check
			ProdStockCallService.checkReturnStockForBill(catalogStockListDtoNew, catalogNum-1, 0, true);
			
			//反库存 幂等
			OrderReturnCatalogDto.CatalogStockListDto catalogStockListDtoNew1 = ProdStockCallService.ReturnStockListForRefundBill(catalogStockListDto,true,true);
			//check
			ProdStockCallService.checkReturnStockForBill(catalogStockListDtoNew1, catalogNum-1, 0, true);
			
			//再次反库存
			catalogStockListDto.setRefundBillNo(refundBillNo+1);
			OrderReturnCatalogDto.CatalogStockListDto catalogStockListDtoNew2 = ProdStockCallService.ReturnStockListForRefundBill(catalogStockListDto);
			//check
			ProdStockCallService.checkReturnStockForBill(catalogStockListDtoNew2, catalogNum, 0, true);
			
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_ReturnStockListForRefundBill_001_001() {
		Logger.start(true, "普通现货商品,删除规格返库存");
		try{
			String productId = SellerProductCallService.addNewProductOnSale(sellerId, sellerName, null).getJSONObject("Data").getString("ProductID");
			
			YmtCatalogsI ymtCatalog = ymtCatalogsIWapper.selectCatalogsByProductId(productId).get(0);
			String catalogId = ymtCatalog.getsCatalogId();
			int catalogNum = ymtCatalog.getiNum();
			int stockNum = 2;
			
			CatalogListDto catalogListDto = ProdStockCallService.ReduceStockListForPreOrder(productId, catalogId, sellerId, 0, stockNum, 0, true);
			ProdStockCallService.checkReduceStock(catalogListDto,catalogNum-stockNum,0);
			
			String catalogsId = new YmtCatalogsIWapper().selectCatalogsByProductId(productId).get(0).getsCatalogId();
			//modify product and on sale 
			Map<String, Object> updateMap = MapUtil.hashMap("LoginID", sellerName, "UserID", sellerId,
															"Product->sProductId",productId,"ProductID",productId,
															"Product->OnSaleType",0,	// 在售类型    Old = -1,  Immediately = 0,    SpecialTime = 1,   OffSale = 2,
															"Product->Catalogs[2]->CatalogId",catalogsId);
			
			String fileName = "changenewproductinfocalltpl_001.json";
			new ChangeNewProductInfoCall().callAndGetReturnData(fileName,updateMap);
			//反库存
			long refundBillNo = getRefundBillNo();
			OrderReturnCatalogDto.CatalogStockListDto catalogStockListDto = new OrderReturnCatalogDto.CatalogStockListDto();
			catalogStockListDto.setRefundBillNo(refundBillNo);
			catalogStockListDto.setBuyerId(catalogListDto.getBuyerId());
			catalogStockListDto.setOrderId(catalogListDto.getPreOrderId());
			catalogStockListDto.setProductId(productId);
			catalogStockListDto.setCatalogId(catalogId);
			catalogStockListDto.setSellerId(sellerId);
			catalogStockListDto.setStockNum(stockNum-1);
			OrderReturnCatalogDto.CatalogStockListDto catalogStockListDtoNew = ProdStockCallService.ReturnStockListForRefundBill(catalogStockListDto);
			//check
			ProdStockCallService.checkReturnStockForBill(catalogStockListDtoNew, catalogNum-1, 0, true);
			
			//反库存 幂等
			OrderReturnCatalogDto.CatalogStockListDto catalogStockListDtoNew1 = ProdStockCallService.ReturnStockListForRefundBill(catalogStockListDto,true,true);
			//check
			ProdStockCallService.checkReturnStockForBill(catalogStockListDtoNew1, catalogNum-1, 0, true);
			
			//再次反库存
			catalogStockListDto.setRefundBillNo(refundBillNo+1);
			OrderReturnCatalogDto.CatalogStockListDto catalogStockListDtoNew2 = ProdStockCallService.ReturnStockListForRefundBill(catalogStockListDto);
			//check
			ProdStockCallService.checkReturnStockForBill(catalogStockListDtoNew2, catalogNum, 0, true);
			
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_ReturnStockListForRefundBill_002() {
		Logger.start(true, "活动商品,没有限购,返库存");
		try {
			String productId = ProductPriceCallService.getProdPriceWithActivity_NewBuyerAndNoIsolation(sellerId).getProductId();
			
			YmtCatalogsI ymtCatalog = ymtCatalogsIWapper.selectCatalogsByProductId(productId).get(0);
			String catalogId = ymtCatalog.getsCatalogId();
			int catalogNum = ymtCatalog.getiNum();
			int stockNum = 2;
			int actNum = ymtProductActivityStockWapper.selectByCatalogId(catalogId).getIstocknum();
			
			int aid = Integer.parseInt(activityProductsWapper.getByProdId(productId).get("aid").toString());
			
			// 扣库存
			CatalogListDto catalogListDto = ProdStockCallService.ReduceStockListForPreOrder(productId, catalogId,
					sellerId, aid, stockNum, 0, true);
			// check
			ProdStockCallService.checkReduceStock(catalogListDto, catalogNum, actNum-stockNum);
			
			
			//反库存
			long refundBillNo = getRefundBillNo();
			OrderReturnCatalogDto.CatalogStockListDto catalogStockListDto = new OrderReturnCatalogDto.CatalogStockListDto();
			catalogStockListDto.setRefundBillNo(refundBillNo);
			catalogStockListDto.setBuyerId(catalogListDto.getBuyerId());
			catalogStockListDto.setOrderId(catalogListDto.getPreOrderId());
			catalogStockListDto.setProductId(productId);
			catalogStockListDto.setCatalogId(catalogId);
			catalogStockListDto.setActivityId(aid);
			catalogStockListDto.setSellerId(sellerId);
			catalogStockListDto.setStockNum(stockNum-1);
			OrderReturnCatalogDto.CatalogStockListDto catalogStockListDtoNew = ProdStockCallService.ReturnStockListForRefundBill(catalogStockListDto);
			//check
			ProdStockCallService.checkReturnStockForBill(catalogStockListDtoNew, catalogNum, actNum-1, true);
			
			
			//反库存 幂等
			OrderReturnCatalogDto.CatalogStockListDto catalogStockListDtoNew1 = ProdStockCallService.ReturnStockListForRefundBill(catalogStockListDto,true,true);
			//check
			ProdStockCallService.checkReturnStockForBill(catalogStockListDtoNew1, catalogNum, actNum-1, true);
		
			//再次反库存
			catalogStockListDto.setRefundBillNo(refundBillNo+1);
			OrderReturnCatalogDto.CatalogStockListDto catalogStockListDtoNew2 = ProdStockCallService.ReturnStockListForRefundBill(catalogStockListDto);
			//check
			ProdStockCallService.checkReturnStockForBill(catalogStockListDtoNew2, catalogNum, actNum, true);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_ReturnStockListForRefundBill_003() {
		Logger.start(true, "活动商品,活动限购,扣减库存,返库存,扣库存");
		try {
			int buyerId = Math.abs(TestDataManager.getRandomIntNum(4));
			String productId = ProductPriceCallService.getProdPriceWithActivity_NewBuyerAndNoIsolation(sellerId).getProductId();
			
			YmtCatalogsI ymtCatalog = ymtCatalogsIWapper.selectCatalogsByProductId(productId).get(0);
			String catalogId = ymtCatalog.getsCatalogId();
			int catalogNum = ymtCatalog.getiNum();
			int stockNum = 2;
			int actNum = ymtProductActivityStockWapper.selectByCatalogId(catalogId).getIstocknum();
			
			int aid = Integer.parseInt(activityProductsWapper.getByProdId(productId).get("aid").toString());
			
			//修改活动限购数量 商品限购
			ProdStockCallService.updateActivityLimit(aid, 0, 3);
			
			// 扣库存
			CatalogListDto catalogListDto = ProdStockCallService.ReduceStockListForPreOrder(productId, catalogId,sellerId, aid, stockNum, 0,buyerId, true);
			// check
			ProdStockCallService.checkReduceStock(catalogListDto, catalogNum, actNum-stockNum);
			
			//反库存
			long refundBillNo = getRefundBillNo();
			OrderReturnCatalogDto.CatalogStockListDto catalogStockListDto = new OrderReturnCatalogDto.CatalogStockListDto();
			catalogStockListDto.setRefundBillNo(refundBillNo);
			catalogStockListDto.setBuyerId(catalogListDto.getBuyerId());
			catalogStockListDto.setOrderId(catalogListDto.getPreOrderId());
			catalogStockListDto.setProductId(productId);
			catalogStockListDto.setCatalogId(catalogId);
			catalogStockListDto.setActivityId(aid);
			catalogStockListDto.setSellerId(sellerId);
			catalogStockListDto.setStockNum(stockNum);
			OrderReturnCatalogDto.CatalogStockListDto catalogStockListDtoNew = ProdStockCallService.ReturnStockListForRefundBill(catalogStockListDto);
			//check
			ProdStockCallService.checkReturnStockForBill(catalogStockListDtoNew, catalogNum, actNum, true);
			
			
			//该用户再下单
			// 扣库存
			CatalogListDto catalogListDto1 = ProdStockCallService.ReduceStockListForPreOrder(productId, catalogId,sellerId, aid, stockNum, 0,buyerId, true);
			// check
			ProdStockCallService.checkReduceStock(catalogListDto1, catalogNum, actNum-stockNum);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_ReturnStockListForRefundBill_004() {
		Logger.start(true, "活动商品,活动商品限购,扣减库存,返库存,扣库存");
		try {
			int buyerId = Math.abs(TestDataManager.getRandomIntNum(4));
			String productId = ProductPriceCallService.getProdPriceWithActivity_NewBuyerAndNoIsolation(sellerId).getProductId();
			
			YmtCatalogsI ymtCatalog = ymtCatalogsIWapper.selectCatalogsByProductId(productId).get(0);
			String catalogId = ymtCatalog.getsCatalogId();
			int catalogNum = ymtCatalog.getiNum();
			int stockNum = 2;
			int actNum = ymtProductActivityStockWapper.selectByCatalogId(catalogId).getIstocknum();
			
			int aid = Integer.parseInt(activityProductsWapper.getByProdId(productId).get("aid").toString());
			
			//修改活动限购数量 商品限购
			ProdStockCallService.updateActivityLimit(aid, 1, 3);
			
			// 扣库存
			CatalogListDto catalogListDto = ProdStockCallService.ReduceStockListForPreOrder(productId, catalogId,sellerId, aid, stockNum, 0,buyerId, true);
			// check
			ProdStockCallService.checkReduceStock(catalogListDto, catalogNum, actNum-stockNum);
			
			//反库存
			long refundBillNo = getRefundBillNo();
			OrderReturnCatalogDto.CatalogStockListDto catalogStockListDto = new OrderReturnCatalogDto.CatalogStockListDto();
			catalogStockListDto.setRefundBillNo(refundBillNo);
			catalogStockListDto.setBuyerId(catalogListDto.getBuyerId());
			catalogStockListDto.setOrderId(catalogListDto.getPreOrderId());
			catalogStockListDto.setProductId(productId);
			catalogStockListDto.setCatalogId(catalogId);
			catalogStockListDto.setActivityId(aid);
			catalogStockListDto.setSellerId(sellerId);
			catalogStockListDto.setStockNum(stockNum);
			OrderReturnCatalogDto.CatalogStockListDto catalogStockListDtoNew = ProdStockCallService.ReturnStockListForRefundBill(catalogStockListDto);
			//check
			ProdStockCallService.checkReturnStockForBill(catalogStockListDtoNew, catalogNum, actNum, true);
			
			
			//该用户再下单
			// 扣库存
			CatalogListDto catalogListDto1 = ProdStockCallService.ReduceStockListForPreOrder(productId, catalogId,sellerId, aid, stockNum, 0,buyerId, true);
			// check
			ProdStockCallService.checkReduceStock(catalogListDto1, catalogNum, actNum-stockNum);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_ReturnStockListForRefundBill_005() {
		Logger.start(true, "活动商品,扣减库存,活动结束返库存");
		try {
			int buyerId = Math.abs(TestDataManager.getRandomIntNum(4));
			String productId = ProductPriceCallService.getProdPriceWithActivity_NewBuyerAndNoIsolation(sellerId).getProductId();
			
			YmtCatalogsI ymtCatalog = ymtCatalogsIWapper.selectCatalogsByProductId(productId).get(0);
			String catalogId = ymtCatalog.getsCatalogId();
			int catalogNum = ymtCatalog.getiNum();
			int stockNum = 2;
			int actNum = ymtProductActivityStockWapper.selectByCatalogId(catalogId).getIstocknum();
			
			int aid = Integer.parseInt(activityProductsWapper.getByProdId(productId).get("aid").toString());
		
			// 扣库存
			CatalogListDto catalogListDto = ProdStockCallService.ReduceStockListForPreOrder(productId, catalogId,sellerId, aid, stockNum, 0,buyerId, true);
			// check
			ProdStockCallService.checkReduceStock(catalogListDto, catalogNum, actNum-stockNum);
			
			//结束活动
			int inaid = new YmtProductsInActivityWapper().selectByProductIdAndActivityId(productId, aid).getIproductinactivityid();
			new CloseProductActivityCall().closeProductInActivity(Arrays.asList(inaid));
			YMTDateUtil.waitTime(5);
			int catalogNumNew = ymtCatalogsIWapper.selectCatalogsByCatalogId(catalogId).getiNum();
			
			//反库存 返共享库存
			long refundBillNo = getRefundBillNo();
			OrderReturnCatalogDto.CatalogStockListDto catalogStockListDto = new OrderReturnCatalogDto.CatalogStockListDto();
			catalogStockListDto.setRefundBillNo(refundBillNo);
			catalogStockListDto.setBuyerId(catalogListDto.getBuyerId());
			catalogStockListDto.setOrderId(catalogListDto.getPreOrderId());
			catalogStockListDto.setProductId(productId);
			catalogStockListDto.setCatalogId(catalogId);
			catalogStockListDto.setActivityId(aid);
			catalogStockListDto.setSellerId(sellerId);
			catalogStockListDto.setStockNum(stockNum);
			OrderReturnCatalogDto.CatalogStockListDto catalogStockListDtoNew = ProdStockCallService.ReturnStockListForRefundBill(catalogStockListDto);
			
			//check
			catalogStockListDtoNew.setActivityId(0);
			ProdStockCallService.checkReturnStockForBill(catalogStockListDtoNew, catalogNumNew+stockNum, 0, true);

			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_ReturnStockListForRefundBill_006() {
		Logger.start(true, "普通FBX商品,扣减库存,反库存");
		try {
			String productId = new AddFbxProductCall().callAndGetReturnData(MapUtil.hashMap("UserID", sellerId, "LoginID", sellerName)).getJSONObject("Data").getString("ProductID");
			
			YmtCatalogsI ymtCatalog = ymtCatalogsIWapper.selectCatalogsByProductId(productId).get(0);
			String catalogId = ymtCatalog.getsCatalogId();
			int catalogNum = ymtCatalog.getiNum();
			int stockNum = 2;
			
			//扣库存
			CatalogListDto catalogListDto = ProdStockCallService.ReduceStockListForPreOrder(productId, catalogId, sellerId, 0, stockNum, 2, true);
			//check
			ProdStockCallService.checkReduceStock(catalogListDto,catalogNum-stockNum,0);
			
			//反库存
			long refundBillNo = getRefundBillNo();
			OrderReturnCatalogDto.CatalogStockListDto catalogStockListDto = new OrderReturnCatalogDto.CatalogStockListDto();
			catalogStockListDto.setRefundBillNo(refundBillNo);
			catalogStockListDto.setBuyerId(catalogListDto.getBuyerId());
			catalogStockListDto.setOrderId(catalogListDto.getPreOrderId());
			catalogStockListDto.setProductId(productId);
			catalogStockListDto.setCatalogId(catalogId);
			catalogStockListDto.setSellerId(sellerId);
			catalogStockListDto.setStockNum(stockNum);
			catalogStockListDto.setCatalogType(2);
			OrderReturnCatalogDto.CatalogStockListDto catalogStockListDtoNew = ProdStockCallService.ReturnStockListForRefundBill(catalogStockListDto);
			//check
			ProdStockCallService.checkReturnStockForBill(catalogStockListDtoNew, catalogNum, 0, true);
			
			//反库存 幂等
			OrderReturnCatalogDto.CatalogStockListDto catalogStockListDtoNew1 = ProdStockCallService.ReturnStockListForRefundBill(catalogStockListDto,true,true);
			//check
			ProdStockCallService.checkReturnStockForBill(catalogStockListDtoNew1, catalogNum, 0, true);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_ReturnStockListForRefundBill_007() {
		Logger.start(true, "FBX活动商品,没有限购,扣库存,返库存");
		try {
			String productId = ProductPriceCallService.getProdPriceWithActivity_NewBuyerAndNoIsolation(sellerId).getProductId();
			
			//修改catalogType=2 FBX
			new YmtProductsIWapper().updateCatalogTypeByProductId(productId, 2);
			new YmtCatalogsIWapper().updateCatalogTypeByProductId(productId, 2);
			
			YmtCatalogsI ymtCatalog = ymtCatalogsIWapper.selectCatalogsByProductId(productId).get(0);
			String catalogId = ymtCatalog.getsCatalogId();
			int catalogNum = ymtCatalog.getiNum();
			int stockNum = 2;
			
			int actNum = ymtProductActivityStockWapper.selectByCatalogId(catalogId).getIstocknum();
			int aid = Integer.parseInt(activityProductsWapper.getByProdId(productId).get("aid").toString());
			
			//扣库存
			CatalogListDto catalogListDto = ProdStockCallService.ReduceStockListForPreOrder(productId, catalogId, sellerId, aid, stockNum, 2, true);
			//check
			ProdStockCallService.checkReduceStock(catalogListDto,catalogNum,actNum-stockNum);
			
			//反库存
			long refundBillNo = getRefundBillNo();
			OrderReturnCatalogDto.CatalogStockListDto catalogStockListDto = new OrderReturnCatalogDto.CatalogStockListDto();
			catalogStockListDto.setRefundBillNo(refundBillNo);
			catalogStockListDto.setBuyerId(catalogListDto.getBuyerId());
			catalogStockListDto.setOrderId(catalogListDto.getPreOrderId());
			catalogStockListDto.setProductId(productId);
			catalogStockListDto.setCatalogId(catalogId);
			catalogStockListDto.setSellerId(sellerId);
			catalogStockListDto.setStockNum(stockNum);
			catalogStockListDto.setActivityId(aid);
			catalogStockListDto.setCatalogType(2);
			OrderReturnCatalogDto.CatalogStockListDto catalogStockListDtoNew = ProdStockCallService.ReturnStockListForRefundBill(catalogStockListDto);
			//check
			ProdStockCallService.checkReturnStockForBill(catalogStockListDtoNew, catalogNum, actNum, true);
			
			//反库存 幂等
			OrderReturnCatalogDto.CatalogStockListDto catalogStockListDtoNew1 = ProdStockCallService.ReturnStockListForRefundBill(catalogStockListDto,true,true);
			//check
			ProdStockCallService.checkReturnStockForBill(catalogStockListDtoNew1, catalogNum, actNum, true);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_ReturnStockListForRefundBill_008() {
		Logger.start(true, "FBX活动商品,活动限购,扣减库存,反库存 ");
		try {
			int buyerId = Math.abs(TestDataManager.getRandomIntNum(4));
			String productId = ProductPriceCallService.getProdPriceWithActivity_NewBuyerAndNoIsolation(sellerId).getProductId();
			
			//修改catalogType=2 FBX
			new YmtProductsIWapper().updateCatalogTypeByProductId(productId, 2);
			new YmtCatalogsIWapper().updateCatalogTypeByProductId(productId, 2);
			
			YmtCatalogsI ymtCatalog = ymtCatalogsIWapper.selectCatalogsByProductId(productId).get(0);
			String catalogId = ymtCatalog.getsCatalogId();
			int catalogNum = ymtCatalog.getiNum();
			int stockNum = 2;
			
			int actNum = ymtProductActivityStockWapper.selectByCatalogId(catalogId).getIstocknum();
			int aid = Integer.parseInt(activityProductsWapper.getByProdId(productId).get("aid").toString());
			
			//修改活动限购数量 商品限购
			ProdStockCallService.updateActivityLimit(aid, 0, 3);
			
			//扣库存
			CatalogListDto catalogListDto = ProdStockCallService.ReduceStockListForPreOrder(productId, catalogId,sellerId, aid, stockNum, 2,buyerId, true);
			//check
			ProdStockCallService.checkReduceStock(catalogListDto, catalogNum, actNum-stockNum);
			
			//反库存
			long refundBillNo = getRefundBillNo();
			OrderReturnCatalogDto.CatalogStockListDto catalogStockListDto = new OrderReturnCatalogDto.CatalogStockListDto();
			catalogStockListDto.setRefundBillNo(refundBillNo);
			catalogStockListDto.setBuyerId(catalogListDto.getBuyerId());
			catalogStockListDto.setOrderId(catalogListDto.getPreOrderId());
			catalogStockListDto.setProductId(productId);
			catalogStockListDto.setCatalogId(catalogId);
			catalogStockListDto.setActivityId(aid);
			catalogStockListDto.setSellerId(sellerId);
			catalogStockListDto.setStockNum(stockNum);
			catalogStockListDto.setCatalogType(2);
			OrderReturnCatalogDto.CatalogStockListDto catalogStockListDtoNew = ProdStockCallService.ReturnStockListForRefundBill(catalogStockListDto);
			//check
			ProdStockCallService.checkReturnStockForBill(catalogStockListDtoNew, catalogNum, actNum, true);
			
			//该用户再下单
			// 扣库存
			CatalogListDto catalogListDto1 = ProdStockCallService.ReduceStockListForPreOrder(productId, catalogId,sellerId, aid, stockNum, 2,buyerId, true);
			// check
			ProdStockCallService.checkReduceStock(catalogListDto1, catalogNum, actNum-stockNum);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_ReturnStockListForRefundBill_009() {
		Logger.start(true, "FBX活动商品,活动商品限购,扣减库存,反库存 ");
		try {
			int buyerId = Math.abs(TestDataManager.getRandomIntNum(4));
			String productId = ProductPriceCallService.getProdPriceWithActivity_NewBuyerAndNoIsolation(sellerId).getProductId();
			
			//修改catalogType=2 FBX
			new YmtProductsIWapper().updateCatalogTypeByProductId(productId, 2);
			new YmtCatalogsIWapper().updateCatalogTypeByProductId(productId, 2);
			
			YmtCatalogsI ymtCatalog = ymtCatalogsIWapper.selectCatalogsByProductId(productId).get(0);
			String catalogId = ymtCatalog.getsCatalogId();
			int catalogNum = ymtCatalog.getiNum();
			int stockNum = 2;
			
			int actNum = ymtProductActivityStockWapper.selectByCatalogId(catalogId).getIstocknum();
			int aid = Integer.parseInt(activityProductsWapper.getByProdId(productId).get("aid").toString());
			
			//修改活动限购数量 商品限购
			ProdStockCallService.updateActivityLimit(aid, 1, 3);
			
			//扣库存
			CatalogListDto catalogListDto = ProdStockCallService.ReduceStockListForPreOrder(productId, catalogId,sellerId, aid, stockNum, 2,buyerId, true);
			//check
			ProdStockCallService.checkReduceStock(catalogListDto, catalogNum, actNum-stockNum);
			
			//反库存
			long refundBillNo = getRefundBillNo();
			OrderReturnCatalogDto.CatalogStockListDto catalogStockListDto = new OrderReturnCatalogDto.CatalogStockListDto();
			catalogStockListDto.setRefundBillNo(refundBillNo);
			catalogStockListDto.setBuyerId(catalogListDto.getBuyerId());
			catalogStockListDto.setOrderId(catalogListDto.getPreOrderId());
			catalogStockListDto.setProductId(productId);
			catalogStockListDto.setCatalogId(catalogId);
			catalogStockListDto.setActivityId(aid);
			catalogStockListDto.setSellerId(sellerId);
			catalogStockListDto.setStockNum(stockNum);
			catalogStockListDto.setCatalogType(2);
			OrderReturnCatalogDto.CatalogStockListDto catalogStockListDtoNew = ProdStockCallService.ReturnStockListForRefundBill(catalogStockListDto);
			//check
			ProdStockCallService.checkReturnStockForBill(catalogStockListDtoNew, catalogNum, actNum, true);
			
			//该用户再下单
			// 扣库存
			CatalogListDto catalogListDto1 = ProdStockCallService.ReduceStockListForPreOrder(productId, catalogId,sellerId, aid, stockNum, 2,buyerId, true);
			// check
			ProdStockCallService.checkReduceStock(catalogListDto1, catalogNum, actNum-stockNum);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_ReturnStockListForRefundBill_010() {
		Logger.start(true, "FBX活动商品,没有限购,扣库存,活动结束,返库存");
		try {
			String productId = ProductPriceCallService.getProdPriceWithActivity_NewBuyerAndNoIsolation(sellerId).getProductId();
			
			//修改catalogType=2 FBX
			new YmtProductsIWapper().updateCatalogTypeByProductId(productId, 2);
			new YmtCatalogsIWapper().updateCatalogTypeByProductId(productId, 2);
			
			YmtCatalogsI ymtCatalog = ymtCatalogsIWapper.selectCatalogsByProductId(productId).get(0);
			String catalogId = ymtCatalog.getsCatalogId();
			int catalogNum = ymtCatalog.getiNum();
			int stockNum = 2;
			
			int actNum = ymtProductActivityStockWapper.selectByCatalogId(catalogId).getIstocknum();
			int aid = Integer.parseInt(activityProductsWapper.getByProdId(productId).get("aid").toString());
			
			//扣库存
			CatalogListDto catalogListDto = ProdStockCallService.ReduceStockListForPreOrder(productId, catalogId, sellerId, aid, stockNum, 2, true);
			//check
			ProdStockCallService.checkReduceStock(catalogListDto,catalogNum,actNum-stockNum);
			
			//结束活动
			int inaid = new YmtProductsInActivityWapper().selectByProductIdAndActivityId(productId, aid).getIproductinactivityid();
			new CloseProductActivityCall().closeProductInActivity(Arrays.asList(inaid));
			YMTDateUtil.waitTime(5);
			int catalogNumNew = ymtCatalogsIWapper.selectCatalogsByCatalogId(catalogId).getiNum();
			
			//反库存
			long refundBillNo = getRefundBillNo();
			OrderReturnCatalogDto.CatalogStockListDto catalogStockListDto = new OrderReturnCatalogDto.CatalogStockListDto();
			catalogStockListDto.setRefundBillNo(refundBillNo);
			catalogStockListDto.setBuyerId(catalogListDto.getBuyerId());
			catalogStockListDto.setOrderId(catalogListDto.getPreOrderId());
			catalogStockListDto.setProductId(productId);
			catalogStockListDto.setCatalogId(catalogId);
			catalogStockListDto.setSellerId(sellerId);
			catalogStockListDto.setStockNum(stockNum);
			catalogStockListDto.setActivityId(aid);
			catalogStockListDto.setCatalogType(2);
			OrderReturnCatalogDto.CatalogStockListDto catalogStockListDtoNew = ProdStockCallService.ReturnStockListForRefundBill(catalogStockListDto);
		
			//check
			catalogStockListDtoNew.setActivityId(0);
			catalogStockListDtoNew.setCatalogType(0);
			ProdStockCallService.checkReturnStockForBill(catalogStockListDtoNew, catalogNumNew+stockNum, 0, true);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_ReturnStockListForRefundBill_999() {
		Logger.start(true, "活动商品,活动限购,扣减库存,返库存,多个订单");
		try {
			
			List<ProductAllPriceBean> productAllPriceBeanList = ProductPriceCallService.getProdPriceListWithActivity_NewBuyerAndNoIsolation(sellerId,2);
			//商品A
			String productId1 = productAllPriceBeanList.get(0).getProductId();
			YmtCatalogsI ymtCatalog1 = ymtCatalogsIWapper.selectCatalogsByProductId(productId1).get(0);
			String catalogId1 = ymtCatalog1.getsCatalogId();
			int catalogNum1 = ymtCatalog1.getiNum();
			int reduceNum1 = 1;
			int actNum1 = ymtProductActivityStockWapper.selectByCatalogId(catalogId1).getIstocknum();
			
			//商品B
			String productId2 = productAllPriceBeanList.get(1).getProductId();
			YmtCatalogsI ymtCatalog2 = ymtCatalogsIWapper.selectCatalogsByProductId(productId2).get(0);
			String catalogId2 = ymtCatalog2.getsCatalogId();
			int catalogNum2 = ymtCatalog2.getiNum();
			int reduceNum2 = 1;
			int actNum2 = ymtProductActivityStockWapper.selectByCatalogId(catalogId2).getIstocknum();

			//修改活动限购数量 商品限购2个
			int aid = Integer.parseInt(activityProductsWapper.getByProdId(productId1).get("aid").toString());
			ProdStockCallService.updateActivityLimit(aid, 0, 2);
			
					
			////////////////////////////////////////////////////////////////////////////////////////////////////
			// 扣库存
			CatalogListDto catalogListDto3 = new CatalogListDto();
			catalogListDto3.setProductId(productId1);
			catalogListDto3.setCatalogId(catalogId1);
			catalogListDto3.setSellerId(sellerId);
			catalogListDto3.setActivityId(aid);
			catalogListDto3.setBuyCount(reduceNum1);
			
			CatalogListDto catalogListDto4 = new CatalogListDto();
			catalogListDto4.setProductId(productId2);
			catalogListDto4.setCatalogId(catalogId2);
			catalogListDto4.setSellerId(sellerId);
			catalogListDto4.setActivityId(aid);
			catalogListDto4.setBuyCount(reduceNum2);
			
			
			ProdStockCallService.ReduceStockListForPreOrder(Arrays.asList(catalogListDto3,catalogListDto4), true, false);
			// check
			ProdStockCallService.checkReduceStock(catalogListDto3, catalogNum1, actNum1-reduceNum1);
			ProdStockCallService.checkReduceStock(catalogListDto4, catalogNum2, actNum2-reduceNum2);
	
			
			////////////////////////////////////////////////////////////////////////////////////////////////////
			//多个订单返
			CatalogStockListDto catalogStockListDto1 = new OrderReturnCatalogDto.CatalogStockListDto();
			catalogStockListDto1.setBuyerId(catalogListDto3.getBuyerId());
			catalogStockListDto1.setOrderId(catalogListDto3.getPreOrderId());
			catalogStockListDto1.setProductId(catalogListDto3.getProductId());
			catalogStockListDto1.setCatalogId(catalogListDto3.getCatalogId());
			catalogStockListDto1.setActivityId(catalogListDto3.getActivityId());
			catalogStockListDto1.setSellerId(sellerId);
			catalogStockListDto1.setStockNum(catalogListDto3.getBuyCount());
			
			CatalogStockListDto catalogStockListDto2 = new OrderReturnCatalogDto.CatalogStockListDto();
			catalogStockListDto2.setBuyerId(catalogListDto4.getBuyerId());
			catalogStockListDto2.setOrderId(catalogListDto4.getPreOrderId());
			catalogStockListDto2.setProductId(catalogListDto4.getProductId());
			catalogStockListDto2.setCatalogId(catalogListDto4.getCatalogId());
			catalogStockListDto2.setActivityId(catalogListDto4.getActivityId());
			catalogStockListDto2.setSellerId(sellerId);
			catalogStockListDto2.setStockNum(catalogListDto4.getBuyCount());
			
			ProdStockCallService.ReturnStockListForRefundBill(Arrays.asList(catalogStockListDto1,catalogStockListDto2));
			//check
			ProdStockCallService.checkReturnStockForBill(catalogStockListDto1, catalogNum1, actNum1, true);
			ProdStockCallService.checkReturnStockForBill(catalogStockListDto2, catalogNum2, actNum2, true);

			//幂等
			ProdStockCallService.ReturnStockListForRefundBill(Arrays.asList(catalogStockListDto1,catalogStockListDto2),true,true);
			ProdStockCallService.checkReturnStockForBill(catalogStockListDto1, catalogNum1, actNum1, true);
			ProdStockCallService.checkReturnStockForBill(catalogStockListDto2, catalogNum2, actNum2, true);
			
			
			////////////////////////////////////////////////////////////////////////////////////////////////////
			//在下单 //重新生成订单号
			catalogListDto3.setPreOrderId(0);
			catalogListDto4.setPreOrderId(0);
			ProdStockCallService.ReduceStockListForPreOrder(Arrays.asList(catalogListDto3,catalogListDto4), true, false);
			// check
			ProdStockCallService.checkReduceStock(catalogListDto3, catalogNum1, actNum1-reduceNum1);
			ProdStockCallService.checkReduceStock(catalogListDto4, catalogNum2, actNum2-reduceNum2);
			
			//一个一个返
			catalogStockListDto2.setRefundBillNo(0);
			catalogStockListDto2.setBuyerId(catalogListDto4.getBuyerId());
			catalogStockListDto2.setOrderId(catalogListDto4.getPreOrderId());
			catalogStockListDto2.setProductId(catalogListDto4.getProductId());
			catalogStockListDto2.setCatalogId(catalogListDto4.getCatalogId());
			catalogStockListDto2.setActivityId(catalogListDto4.getActivityId());
			catalogStockListDto2.setSellerId(sellerId);
			catalogStockListDto2.setStockNum(catalogListDto4.getBuyCount());
			ProdStockCallService.ReturnStockListForRefundBill(Arrays.asList(catalogStockListDto2));
			//check
			ProdStockCallService.checkReturnStockForBill(catalogStockListDto2, catalogNum1, actNum1, true);
			
			//幂等
			ProdStockCallService.ReturnStockListForRefundBill(Arrays.asList(catalogStockListDto2),true,true);
			ProdStockCallService.checkReturnStockForBill(catalogStockListDto2, catalogNum1, actNum1, true);
			
			
			
			catalogStockListDto1.setRefundBillNo(0);
			catalogStockListDto1.setBuyerId(catalogListDto3.getBuyerId());
			catalogStockListDto1.setOrderId(catalogListDto3.getPreOrderId());
			catalogStockListDto1.setProductId(catalogListDto3.getProductId());
			catalogStockListDto1.setCatalogId(catalogListDto3.getCatalogId());
			catalogStockListDto1.setActivityId(catalogListDto3.getActivityId());
			catalogStockListDto1.setSellerId(sellerId);
			catalogStockListDto1.setStockNum(catalogListDto3.getBuyCount());
			ProdStockCallService.ReturnStockListForRefundBill(Arrays.asList(catalogStockListDto1));
			//check
			ProdStockCallService.checkReturnStockForBill(catalogStockListDto1, catalogNum1, actNum1, true);
			
			//幂等
			ProdStockCallService.ReturnStockListForRefundBill(Arrays.asList(catalogStockListDto1),true,true);
			ProdStockCallService.checkReturnStockForBill(catalogStockListDto1, catalogNum1, actNum1, true);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	public static long getRefundBillNo(){
		return Long.parseLong(YMTDateUtil.getSysTime("yyyyMMddHHmmssSSS"));
//		return Long.parseLong(YMTDateUtil.getSysTime("HmmssSSS"));
	}
	
}
